﻿
//Author: 张亚涛
//createtime:2016-01-14 21:12:31
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "MemberRelation" 表的方法;
    /// </summary>
    public class MemberRelationBase
    {
        const string SQL_Select = "select [MemberID],[PartnerID],[PartnerID2],[PartnerID3],[RelationDate] from [MemberRelation] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回MemberRelation表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<MemberRelation> GetAllMemberRelation()
        {
            List<MemberRelation> memberRelationList = new List<MemberRelation>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                MemberRelation memberRelation = new MemberRelation(reader);
                memberRelationList.Add(memberRelation);
            }
            reader.Close();
            return memberRelationList;
        }
        /// <summary>
        /// 根据MemberRelation表的MemberID字段返回数据 
        /// </summary>
        /// <param name="memberID">MemberID</param>
        /// <returns></returns>       
        public static MemberRelation GetMemberRelationByOpenID(string openID)
        {
            MemberRelation memberRelation = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [MemberID] = @MemberID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@MemberID",openID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                memberRelation = new MemberRelation(reader);
            }
            reader.Close();
            return memberRelation;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据MemberRelation表的MemberID字段删除数据 
        /// </summary>
        /// <param name="memberID">memberID</param>
        /// <returns></returns>        
        public static int DeleteMemberRelationByMemberID(string memberID)
        {
            string sql = "delete from [MemberRelation] where [MemberID] = @MemberID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@MemberID",memberID)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(MemberRelation memberRelation)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@MemberID",memberRelation.MemberID),
            new SqlParameter("@PartnerID",memberRelation.PartnerID),
            new SqlParameter("@PartnerID2",memberRelation.PartnerID2),
            new SqlParameter("@PartnerID3",memberRelation.PartnerID3),
            new SqlParameter("@RelationDate",memberRelation.RelationDate)    
            };
            return paras;
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向MemberRelation表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="memberRelation">MemberRelation</param>       
        /// <returns></returns>        
        public static int AddMemberRelation(MemberRelation memberRelation)
        {
            string sql = "insert into [MemberRelation]([MemberID],[PartnerID],[PartnerID2],[PartnerID3],[RelationDate])values(@MemberID,@PartnerID,@PartnerID2,@PartnerID3,@RelationDate)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(memberRelation);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 向MemberRelation表插入一条数据 （SQL语句）
        /// </summary>
        /// <param name="m">MemberRelation</param>       
        /// <returns></returns>        
        public static string AddMemberRelation_SQL(MemberRelation m)
        {
            string sql = "insert into [MemberRelation]([MemberID],[PartnerID],[PartnerID2],[PartnerID3],[RelationDate])";
            sql += " values('" + m.MemberID + "','" + m.PartnerID + "','" + m.PartnerID2 + "','" + m.PartnerID3 + "','" + m.RelationDate.ToString() + "');";

            return sql;
        }
        /// <summary>
        /// 返回MemberRelation表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllMemberRelation_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}